package com.example.xxljobdemo;

public class YieldExample implements Runnable {

    public void run() {
        for (int i = 1; i <= 500; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + i);
            // 每次循环执行完后调用yield()方法让出CPU时间
            Thread.yield();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new YieldExample(), "Thread 1");
        Thread t2 = new Thread(new YieldExample(), "Thread 2");

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        for (int i = 1; i <= 500; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + i);
            // 每次循环执行完后调用yield()方法让出CPU时间
            Thread.yield();
        }
    }
}
